package com.digitalchina.lxnsy.activity.old;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessPersonnelService;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.entity.SheepVO;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.FrescoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.facebook.drawee.view.SimpleDraweeView;

import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * ================================================
 * 项目名称：BigData
 * 类描述：
 * 包名：com.digitalchina.bigdata.activity
 * 创建人：郑皓
 * 创建时间：2017/9/9 15:30
 * 修改备注：
 * ================================================
 */
public class SheepDetailsActivity extends BaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.iv_head)
    SimpleDraweeView ivHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_price)
    TextView tvPrice;
    @Bind(R.id.tv_people_num)
    TextView tvPeopleNum;
    @Bind(R.id.tv_start_date)
    TextView tvStartDate;
    @Bind(R.id.tv_tel)
    TextView tvTel;
    @Bind(R.id.tv_address)
    TextView tvAddress;
    @Bind(R.id.layout_chat_tel)
    LinearLayout layoutChatTel;
    @Bind(R.id.sdv_tel)
    SimpleDraweeView sdvTel;
    @Bind(R.id.layout_tel)
    LinearLayout layoutTel;
    @Bind(R.id.layout_work_time)
    RelativeLayout layoutWorkTime;
    @Bind(R.id.view_work_time)
    View viewWorkTime;
    private SheepVO sheepVo;
    private String postType = "";

    public static final String OPERATIONVIEW = "0";
    public static final String OPERATIONCHAT = "1";
    public static final String OPERATIONTEL = "2";

    @Override
    public void setLayout() {
        setContentLayout(R.layout.activity_sheep_details);
        setTitle("良种羊详情");
    }

    @Override
    public void initMember() {
        String id = "";
        if (getIntent().hasExtra("SheepVO")) {
            Map<String, Object> map = (Map<String, Object>) getIntent().getSerializableExtra("SheepVO");
            sheepVo = (SheepVO) map.get("vo");
            postType = map.get("postType") + "";
            id = sheepVo.id;
        }
        BusinessPersonnelService.getSheepInfo(this, id, mHandler);
    }

    private void setView(SheepVO sheepVo) {
        BusinessPersonnelService.saveSheepOperation(this, sheepVo.getId(), mHandler);

        if (sheepVo != null && sheepVo.isMyTalents())
            layoutChatTel.setVisibility(View.GONE);

        tvTitle.setText(sheepVo.getContent() + "");
        tvName.setText(sheepVo.getContacts() + "");
        tvTime.setText(sheepVo.getUpdateTime() + "");
        tvPeopleNum.setText(sheepVo.getNumber() + "");
        if (sheepVo.getStartDate() != null && sheepVo.getEndDate() != null) {
            layoutWorkTime.setVisibility(View.VISIBLE);
            viewWorkTime.setVisibility(View.VISIBLE);
            tvStartDate.setText(sheepVo.getStartDate()+ "至" + sheepVo.getEndDate());
        } else {
            layoutWorkTime.setVisibility(View.GONE);
            viewWorkTime.setVisibility(View.GONE);
        }

        tvTel.setText(sheepVo.getTelephone() + "");

        if(sheepVo.getPrice()==0){
            tvPrice.setText( "面议");

        }else{
            tvPrice.setText(sheepVo.getPrice() + "元/只");
        }

        tvAddress.setText(sheepVo.getAddress() + "");
    }

    @Override
    public void addListener() {

    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case MSG.MSG_GET_TALENTS_INFO_SUCCESS:
                        callBack(msg.obj);
                        break;
                }
            }
        };
    }

    @Override
    public void callBack(Object object) {
        sheepVo = FastJsonUtil.getBeanData(object.toString(),  SheepVO.class);
        if (sheepVo != null)
            setView(sheepVo);
    }

    @Override
    public void rightOnClick() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @OnClick({R.id.layout_tel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.layout_tel:
                if (sheepVo != null && !StringUtil.isStrEmpty(sheepVo.getTelephone())) {
                    Utils.callPhone(SheepDetailsActivity.this, sheepVo.getTelephone());
                    BusinessPersonnelService.saveSheepTelOperation(this, sheepVo.getId(), new Handler());
                }

                break;
        }
    }
}
